<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="commodityId">
        <el-select v-model="queryParams.commodityId" placeholder="请选择商品名称" clearable size="small">
          <el-option
            v-for="item in commoditys"
            :key="item.commoditycode"
            :label="item.productname"
            :value="item.commoditycode"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="购买人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择购买人" clearable size="small">
          <el-option
            v-for="item in users"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择商品类型" clearable size="small">
          <el-option
            v-for="item in commodityTypes"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择商品状态" clearable size="small">
          <el-option
            v-for="item in commodityStatuss"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:log:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:log:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单号" align="center" prop="orderId"/>
      <el-table-column label="商品编码" align="center" prop="commodityId"/>
      <el-table-column label="商品名称" align="center" prop="commodityName"/>
      <el-table-column label="下单人" align="center" prop="orderPlacer"/>
      <el-table-column label="订单类型" align="center" prop="orderType"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus"/>
      <el-table-column label="下单时间" align="center" prop="createTime"/>
      <el-table-column label="超时时间" align="center" prop="timeout"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:log:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/system/order";
  import {listUser} from "@/api/system/user";
  import {listCommodity} from "@/api/system/commodity";

  export default {
    name: "log",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单操作记录表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderSettingId: null,
          userId: null,
          orderType: null,
          commodityId: null,
          commodityName: null,
          orderStatus: null
        },
        users: [],
        commoditys: [],
        commodityTypes: [
          {id: 1, name: "秒杀订单"},
          {id: 2, name: "正常订单"}
        ],
        commodityStatuss: [
          {
            id: 1,
            name: "未支付"
          },
          {
            id: 2,
            name: "已支付"
          },
          {
            id: 3,
            name: "已过期"
          },
          {
            id: 4,
            name: "已发货"
          },
          {
            id: 5,
            name: "已确认收货"
          },
          {
            id: 6,
            name: "已评价"
          },
        ],
        // 表单参数
        form: {},
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询订单操作记录列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.orderList.forEach(order => {
            if (order.orderType == 1) {
              order.orderType = "秒杀订单"
            }
            if (order.orderType == 2) {
              order.orderType = "正常订单"
            }

            if (order.orderStatus == 1) {
              order.orderStatus = "未支付"
            } else if (order.orderStatus == 2) {
              order.orderStatus = "已支付"
            } else if (order.orderStatus == 3) {
              order.orderStatus = "已过期"
            } else if (order.orderStatus == 4) {
              order.orderStatus = "已发货"
            } else if (order.orderStatus == 5) {
              order.orderStatus = "已确认收货"
            } else if (order.orderStatus == 6) {
              order.orderStatus = "已评价"
            }

          })

          this.total = response.total;
          this.loading = false;
        });
        listUser().then(rsp => {
          this.users = rsp.rows;
        })
        listCommodity().then(rsp => {
          this.commoditys = rsp.rows;
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          commodityId: null,
          operateMan: null,
          createTime: null,
          commodityStatus: 0,
          note: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单操作记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getOrder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改订单操作记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateOrder(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOrder(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除订单操作记录编号为"' + ids + '"的数据项？').then(function () {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/log/export', {
          ...this.queryParams
        }, `log_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
